<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Create a new fileinfo resource</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.finfo-file.html">finfo_file</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.finfo-set-flags.html">finfo_set_flags</a></div>
 <div class="up"><a href="ref.fileinfo.html">Fileinfo 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.finfo-open" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">finfo_open</h1>
  <h1 class="refname">finfo::__construct</h1>
  <p class="verinfo">(PHP &gt;= 5.3.0, PECL fileinfo &gt;= 0.1.0)</p><p class="refpurpose"><span class="refname">finfo_open</span> -- <span class="refname">finfo::__construct</span> &mdash; <span class="dc-title">Create a new fileinfo resource</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.finfo-open-description">
  <h3 class="title">说明</h3>
  <p class="para">过程化风格</p>
  <div class="methodsynopsis dc-description">
   <span class="type">resource</span> <span class="methodname"><strong>finfo_open</strong></span>
    ([ <span class="methodparam"><span class="type">int</span> <code class="parameter">$options</code><span class="initializer"> = FILEINFO_NONE</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$magic_file</code><span class="initializer"> = <strong><code>NULL</code></strong></span></span>
  ]] )</div>

  <p class="para rdfs-comment">面向对象风格 (constructor):</p>
  <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>finfo::__construct()</strong></span>
    ([ <span class="methodparam"><span class="type">int</span> <code class="parameter">$options</code><span class="initializer"> = FILEINFO_NONE</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$magic_file</code><span class="initializer"> = <strong><code>NULL</code></strong></span></span>
  ]] )</div>

  <p class="para rdfs-comment">
   This function opens a magic database and returns its resource.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.finfo-open-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">options</code></em></dt>

     <dd>

      <p class="para">
       One or disjunction of more <a href="fileinfo.constants.html" class="link">Fileinfo
       constants</a>.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">magic_file</code></em></dt>

     <dd>

      <p class="para">
       Name of a magic database file, usually something like
       <var class="filename">/path/to/magic.mime</var>. If not specified, the
       <em>MAGIC</em> environment variable is used. If the
       environment variable isn&#039;t set, then PHP&#039;s bundled magic database will
       be used.
      </p>
      <p class="para">
       Passing <strong><code>NULL</code></strong> or an empty string will be equivalent to the default
       value.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.finfo-open-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   (Procedural style only)
   Returns a magic database resource on success 或者在失败时返回 <strong><code>FALSE</code></strong>.
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.finfo-open-notes">
  <h3 class="title">注释</h3>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    The expected magic database format changed in PHP 5.3.11 and 5.4.1.
    Due to this, the internal magic database was upgraded. This mostly effects
    code where an external magic database is used: reading an older magic file
    will now fail.
    
    Also, some textual representations of the mime types has changed, for
    instance for PHP would be &quot;PHP script, ASCII text&quot; instead of &quot;PHP script
    text&quot; returned.
   </p>
  </div>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Generally, using the bundled magic database (by leaving
    <em><code class="parameter">magic_file</code></em> and the <em>MAGIC</em>
    environment variables unset) is the best course of action unless you
    specifically need a custom magic database.
   </p>
  </p></blockquote>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.finfo-open-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-2280">
   <p><strong>Example #1 面向对象风格</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$finfo&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">finfo</span><span style="color: #007700">(</span><span style="color: #0000BB">FILEINFO_MIME</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"/usr/share/misc/magic"</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;return&nbsp;mime&nbsp;type&nbsp;ala&nbsp;mimetype&nbsp;extension<br /><br />/*&nbsp;get&nbsp;mime-type&nbsp;for&nbsp;a&nbsp;specific&nbsp;file&nbsp;*/<br /></span><span style="color: #0000BB">$filename&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"/usr/local/something.txt"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$finfo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">file</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

  </div>
  <div class="example" id="example-2281">
   <p><strong>Example #2 过程化风格</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$finfo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">finfo_open</span><span style="color: #007700">(</span><span style="color: #0000BB">FILEINFO_MIME</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"/usr/share/misc/magic"</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;return&nbsp;mime&nbsp;type&nbsp;ala&nbsp;mimetype&nbsp;extension<br /><br /></span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">$finfo</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Opening&nbsp;fileinfo&nbsp;database&nbsp;failed"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;exit();<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;get&nbsp;mime-type&nbsp;for&nbsp;a&nbsp;specific&nbsp;file&nbsp;*/<br /></span><span style="color: #0000BB">$filename&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"/usr/local/something.txt"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">finfo_file</span><span style="color: #007700">(</span><span style="color: #0000BB">$finfo</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$filename</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;close&nbsp;connection&nbsp;*/<br /></span><span style="color: #0000BB">finfo_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$finfo</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
  <p class="para">以上例程会输出：</p>
  <div class="example-contents screen">
<div class="cdata"><pre>
text/plain; charset=us-ascii
</pre></div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.finfo-open-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.finfo-close.html" class="function" rel="rdfs-seeAlso">finfo_close()</a> - Close fileinfo resource</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.finfo-file.html">finfo_file</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.finfo-set-flags.html">finfo_set_flags</a></div>
 <div class="up"><a href="ref.fileinfo.html">Fileinfo 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
